import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		token: '', //...
		deviceUUID: '', //设备的UUID
		isNet: true,
		isConnected: false,
		isEiDevice: true,
		isBluetoothAdapter: true,
		isActiveDisConnect: false,  //是否主动断开蓝牙设备
		originalData: [], //原始数据
		userInfo: null, //用户信息 多个人的信息
		userBaseInfo: null, //用户个人基础信息
		// dfu 相关
		isDFU: false, //是否在DFU
		bleId: '', //蓝牙设备的mac/id 地址
		isBuildModel: null, //是否建模
		birthday: '',
		mappingRatio: 0.8,  //映射比
		deepWarningTimes: 300,
		isLoopCheckConnectState: true,
		isLoopCheckAdornState: true,  //是否循环检测头环佩戴情况
		firmwareVersion: '1.0.0',   //蓝牙设备的固件版本
		isReceipt: false,
		receiptTimer: null,
		isRecvMsg: false, //是否能接收到检测数据
		isCommunicationFail: false,
	},
	mutations: {
		token(state, stauts) {
			try {
				state.token = stauts;
			} catch (e) {
				console.log(e);
			}
		},
		// UUID
		deviceUUID(state, stauts) {
			state.deviceUUID = stauts;
		},
		firmwareVersion(state, stauts) {
			state.firmwareVersion = stauts;
		},
		// 监听网络情况
		changeNetStatus(state, stauts) {
			state.isNet = stauts;
		},
		connectState(state, connectState) {
			state.isConnected = connectState;
		},
		isBuildModel(state, stauts) {
			state.isBuildModel = stauts;
		},
		isActiveDisConnect(state, stauts) {
			state.isActiveDisConnect = stauts;
		},
		isLoopCheckConnectState(state, stauts) {
			state.isLoopCheckConnectState = stauts;
		},
		isLoopCheckAdornState(state, stauts) {
			state.isLoopCheckAdornState = stauts;
		},
		userInfo(state, userInfo) {
			try {
				state.userInfo = userInfo;
				uni.setStorageSync('userInfo', userInfo);
			} catch (e) {
				console.log(e);
			}
		},
		userBaseInfo(state, userBaseInfo) {
			state.userBaseInfo = userBaseInfo;
		},
		updateCurrentUser(state, currentUser) {
			state.userInfo.currentUser = currentUser;
		},
		UpdateAppSecret(state, AppSecret) {
			state.userInfo.currentUser.appSecret = AppSecret;
			state.userInfo.loginUserList.forEach(item => {
				if (item.userId == state.userInfo.currentUser.userId) item.appSecret = AppSecret;
			})
			try {
				uni.setStorageSync('userInfo', state.userInfo);
			} catch (e) {
				console.log(e);
			}
		},
		BluetoothAdapterState(state, isAdapter) {
			state.isBluetoothAdapter = isAdapter;
		},
		isEiDevice(state, iseiDevice) {
			state.isEiDevice = iseiDevice;
		},
		originalData(state, originalData) {
			let array = Array.prototype.slice.call(new Uint8Array(originalData));
			state.originalData = array;
		},
		isDFU(state, stauts) {
			state.isDFU = stauts;
		},
		bleId(state, stauts) {
			state.bleId = stauts;
		},
		SET_MappingRatio(state,value) {
			state.mappingRatio = value
			try {
				uni.setStorageSync('mappingRatio', value);
			} catch (e) {
				console.log(e);
			}
		},
		// deepWarningTimes
		SET_DeepWarningTimes(state,value) {
			state.deepWarningTimes = value
			try {
				uni.setStorageSync('deepWarningTimes', value);
			} catch (e) {
				console.log(e);
			}
		},
		receiptTimer(state, stauts) {
			state.receiptTimer = stauts;
		},
		isReceipt(state, stauts) {
			state.isReceipt = stauts;
		},
		isRecvMsg(state, stauts) {
			state.isRecvMsg = stauts;
		},
		isCommunicationFail(state, stauts) {
			state.isCommunicationFail = stauts;
		},
	},
	actions: {
		updateOriginalData(context, originalData) {
			context.commit('originalData', originalData)
		}
	}
})

export default store